package com.stylefeng.guns;

import com.stylefeng.guns.config.properties.GunsProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * SpringBoot方式启动类
 *
 * @author stylefeng
 * @Date 2017/5/21 12:06
 */
@SpringBootApplication
public class GunsApplication extends WebMvcConfigurerAdapter{

    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    @Autowired
    GunsProperties gunsProperties;

    /**
     * 跟着程序运行一次
     * @param platformTransactionManager
     * @return
     */
    @Bean
    public Object getDatasource(PlatformTransactionManager platformTransactionManager){
        // org.springframework.jdbc.datasource.DataSourceTransactionManager
        System.out.println("GunsApplication >>>>>>>>>> getDatasource:" + platformTransactionManager.getClass().getName());
        return new Object();
    }

    /**
     * 增加swagger的支持
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(gunsProperties.getSwaggerOpen()){
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    public static void main(String[] args) {
        ApplicationContext appctx =
                SpringApplication.run(GunsApplication.class, args);
        logger.info("appctx.getBeanDefinitionCount = " , appctx.getBeanDefinitionCount());
        /*程序运行起来，页面访问不了*/
//        try {
//             ((ConfigurableApplicationContext) appctx).close();
//         } catch (Exception e) { /*ignore*/ }
        logger.info("GunsApplication is success!");
    }
}
